#pragma once
#include <deque>
#include <map>
#include "../../../include/objpool.hpp"
#include "../../../include/timer_interface.hpp"
#include "../../../include/lock.hpp"
#include "iocpsocket.h"

class CIocpNetMgr;

typedef std::deque<char*> CBufferDeque;
typedef std::map<UINT32, CBufferDeque > CBufferPool;

class CIocpSocketMgr :
    public ITimerResponse
{
public:
    CIocpSocketMgr(CIocpNetMgr* iocp_net_mgr);
    ~CIocpSocketMgr(void);

    bool Init(void);
    void UnInit(void);

    CIocpSocket* AllocSocket(void);
    void FreeSocket(CIocpSocket* iocp_socket);

    char* AllocBuf(UINT32 dwSize);
    void FreeBuf(char* pBuf, UINT32 dwSize);

    CIocpListener* AllocListener(void);
    void FreeListener(CIocpListener* listener);

    CIocpSocket* Session2Socket(HSESSION session);
    CIocpListener* NetHandle2Listener(HNETHANDLE net_handle);

    bool Update(void);

    bool ModTimer(CIocpSocket& iocp_socket, INT32 elapse, INT32 count);

    void DelTimer(CIocpSocket& iocp_socket);

    virtual void OnTimer(HTIMER hTimer, void* pData);

protected:
    ITimerManager*			m_timer_mgr;
    CObjPool<CIocpSocket>   m_pool_iocp_socket;
    CObjPool<CIocpListener> m_pool_listener;
    CIocpNetMgr*			m_iocp_net_mgr;
    CBufferPool             m_oBufPool;

    CThreadLock             m_alloc_socket_lock;
    CThreadLock             m_alloc_buf_lock;
};
